package org.jeecg.modules.demo.crm.service.impl;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.crm.dto.ApplyAuditParametersDTO;
import org.jeecg.modules.demo.crm.entity.CInvoiceApply;
import org.jeecg.modules.demo.crm.entity.CInvoiceApplyAsk;
import org.jeecg.modules.demo.crm.mapper.CInvoiceApplyAskMapper;
import org.jeecg.modules.demo.crm.mapper.CInvoiceApplyMapper;
import org.jeecg.modules.demo.crm.service.ICInvoiceApplyAskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Description: 开票申请
 * @Author: jeecg-boot
 * @Date:   2025-06-23
 * @Version: V1.0
 */
@Service
public class CInvoiceApplyAskServiceImpl extends ServiceImpl<CInvoiceApplyAskMapper, CInvoiceApplyAsk> implements ICInvoiceApplyAskService {

    @Autowired
    private CInvoiceApplyAskMapper cInvoiceApplyAskMapper;

    @Autowired
    private CInvoiceApplyMapper cInvoiceApplyMapper;

    @Override
    @Transactional
    public void audit(CInvoiceApplyAsk cInvoiceApplyAsk) {
        CInvoiceApplyAsk cInvoiceApplyAsk1 = cInvoiceApplyAskMapper.selectById(cInvoiceApplyAsk.getId());
        String status = cInvoiceApplyAsk.getStatus();
        if("待审批".equals(cInvoiceApplyAsk1.getStatus()) ){
            if("同意".equals(status)){
                cInvoiceApplyAsk1.setStatus("待开票");
            }else {
                cInvoiceApplyAsk1.setStatus("驳回");
            }
            cInvoiceApplyAsk1.setReviewDescription(cInvoiceApplyAsk.getReviewDescription());
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            cInvoiceApplyAsk1.setReviewerUserId(sysUser.getId());
            cInvoiceApplyAsk1.setReviewTime(new Date());
            cInvoiceApplyAskMapper.updateById(cInvoiceApplyAsk1);
        }else {
            cInvoiceApplyAsk.setStatus("待审批");
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            cInvoiceApplyAsk.setSubmitterUserId(sysUser.getId());
            cInvoiceApplyAsk.setSubmissionTime(new Date());
            cInvoiceApplyAskMapper.updateById(cInvoiceApplyAsk);
        }
    }

    @Override
    public void appplyAudit(ApplyAuditParametersDTO applyAuditParametersDTO) {
        CInvoiceApplyAsk cInvoiceApplyAsk = cInvoiceApplyAskMapper.selectById(applyAuditParametersDTO.getApplyAskId());
        CInvoiceApply cInvoiceApply = cInvoiceApplyMapper.selectById(applyAuditParametersDTO.getApplyId());
        if(cInvoiceApplyAsk != null && cInvoiceApplyAsk.getStatus().equals("待开票") && cInvoiceApply != null){

            cInvoiceApplyAsk.setStatus("已开票");
            cInvoiceApplyAsk.setFinancialTime(new Date());
            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
            cInvoiceApplyAsk.setFinancialUserId(sysUser.getId());
            cInvoiceApplyAsk.setApplyId(applyAuditParametersDTO.getApplyId());
            cInvoiceApplyAskMapper.updateById(cInvoiceApplyAsk);

            //绑定发票
            //cInvoiceApply.setApplyAskId(applyAuditParametersDTO.getApplyAskId());
            //cInvoiceApplyMapper.updateById(cInvoiceApply);
        }


    }
}
